/*
 * BaseRefreshListFragment.java
 * classes : com.ruint.core.view.BaseRefreshListFragment
 * @author ruint
 * V 1.0.0
 * Create at 2014-12-2 下午2:26:43
 */
package com.ruint.core.view;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ruint.core.adapter.BaseRefreshListAdapter;
import com.ruint.core.interfaces.IRefreshFragment;
import com.ruint.corelib.R;
import com.ruint.lib.pulltorefresh.library.PullToRefreshBase;
import com.ruint.lib.pulltorefresh.library.PullToRefreshBase.Mode;
import com.ruint.lib.pulltorefresh.library.PullToRefreshBase.OnAdvRefreshListener;
import com.ruint.lib.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.ruint.lib.pulltorefresh.library.PullToRefreshListView;

/**
 * com.ruint.core.view.BaseRefreshListFragment
 * 
 * @author ruint <br>
 *         create at 2014-12-2 下午2:26:43
 * @param <V>
 */
public abstract class BaseRefreshListFragment<T, V extends BaseRefreshListAdapter<T, PullToRefreshListView>> extends
    Fragment implements OnAdvRefreshListener<ListView>, OnRefreshListener<ListView>, IRefreshFragment<T, V> {

  protected static final String ARG_LIST_DATA = "ARG_LIST_DATA";

  protected PullToRefreshListView mListView;
  protected V mAdapter;
  protected List<T> mData;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_pull_list, container, false);
    mListView = (PullToRefreshListView) rootView.findViewById(R.id.pull_list);
    setRefreshMode();
    setAdapter();
    if (refreshMode() == Mode.BOTH) {
      setAdvRefreshListener(this);
    } else {
      setRefreshListener(this);
    }
    loadData();
    return rootView;
  }

  private void setAdvRefreshListener(OnAdvRefreshListener<ListView> listener) {
    mListView.setOnRefreshListener(listener);
  }

  private void setRefreshListener(OnRefreshListener<ListView> listener) {
    mListView.setOnRefreshListener(listener);
  }

  private void setAdapter() {
    mAdapter = newAdapter();
    mListView.setAdapter(mAdapter);
  }

  private void setRefreshMode() {
    mListView.setMode(refreshMode());
  }

  private void loadData() {
    if (refreshMode() == Mode.BOTH) {
      headRefresh();
    } else {
      onListRefresh();
    }
  }

  protected List<T> getData() {
    return mData;
  }

  @Override
  public void injectData(List<T> data) {
    mData = data;
    mAdapter.refresh(mData);
  }

  @Override
  public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    onListRefresh();
  }

  @Override
  public void onHeadRefresh(PullToRefreshBase<ListView> refreshView) {
    headRefresh();
  }

  @Override
  public void onFootRefresh(PullToRefreshBase<ListView> refreshView) {
    footRefresh();
  }

}
